package com.servlet;

import java.io.IOException;


import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.MessageDAO;
import com.model.Message;
import com.model.Users;


@WebServlet("/messagemanager")
public class MessageManagerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
		
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String func = request.getParameter("func");
		if ("add".equals(func)) 
			add(request, response);
		else if ("delete".equals(func))
			delete(request, response);
		else if ("edit".equals(func))
			edit(request, response);
		else if ("update".equals(func))
			update(request, response);
		else show(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		Users loginUser=(Users)request.getSession().getAttribute("loginUser");
		Message obj = new Message();
		obj.setTitle(title);
		obj.setContent(content);
		obj.setName(loginUser.getUsername());
		MessageDAO messageDao = new MessageDAO();
	   	messageDao.insert(obj);
		show(request, response);
	}
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		new MessageDAO().delete(Integer.parseInt(id));
		show(request, response);
	} 
	
	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		request.setAttribute("message", new MessageDAO().get(Integer.parseInt(id)));
		request.getRequestDispatcher("/message/editMessage.jsp").forward(request,
				response);
	} 
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		Message obj = new Message();
		obj.setId(Integer.parseInt(id));
		obj.setName(name);
		obj.setTitle(title);
		obj.setContent(content);
		new MessageDAO().update(obj);
		show(request, response);
	} 
	private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MessageDAO messageDao = new MessageDAO();
		String msg=(String)request.getParameter("msg");
		System.out.println("msg"+msg);
		
		HttpSession session=request.getSession();
		String pn=request.getParameter("pageNumber");
		
		List<Message> list =null;
		if(msg==null) {
			msg=(String)session.getAttribute("msg");
			if(msg==null)msg="all";
		}
		session.setAttribute("msg",msg);
		if("my".equals(msg)){
			Users u=(Users)request.getSession().getAttribute("loginUser");
		}
		request.getRequestDispatcher("/message/showMessages.jsp").forward(request, response);
	}
}
